Grokking Python Fundamentals
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Course Overview
Python Basics
Python Overview
Python Application Areas
Your First Python Program
Python - Basic Syntax
Python Comments
Quiz
Python Variables and Operators
Python - Variables
Python - Data types
Python - Numbers
Python - Booleans
Python - Type Conversions
Python - Operators
Quiz
Python Conditionals
Introduction to Conditional Statements in Python
Python - If...Else
Python - Match Case
Quiz
Python loops
Introduction to loops in Python
Python - for loop
Python - while loop
Python - Loop Control
Python - for else and while else loop
Quiz
Python Strings
Introduction to Strings in Python
Python - String Operators
Python Slicing Strings
Python - String Methods
Quiz
Python Array
Introduction to Array in Python
Python - Array Methods
Quiz
Python Lists
Introduction to Lists in Python
Python - Add and Remove List Items
Python - List Comprehension
Python - List Methods
Quiz
Python Tuples
Introduction to Tuple in Python
Python - Unpack Tuple Items
Python - Join Tuples
Python - Tuple Methods
Quiz
Python -Dictionaries
Introduction to Dictionary in Python
Python - Nested Dictionaries
Python - Dictionary Methods
Quiz
Python -Sets
Introduction to set in Python
Python - set Operations
Python - Set Methods
Quiz
Introductions to Function in Python
Python - Function Arguments
Recursive Functions
Python - Variable scope
Importing Modules
Quiz
Python - Set Methods

Python sets come with a comprehensive set of methods that allow for powerful and efficient manipulation of set data. These methods facilitate operations ranging from adding and removing elements to comparing sets and performing mathematical set operations.

Set Methods

Here is a table of the most commonly used set methods along with rephrased descriptions for better clarity:

MethodDescriptionTime Complexity
add()Adds an element to the set, if it is not already present.
clear()Empties the set, removing all elements.
copy()Creates a copy of the set that is independent of the original.
difference()Returns a new set containing elements in the first set but not in others.
difference_update()Modifies the set by removing elements found in other sets.
discard()Removes a specified element from the set if it is present.
intersection()Returns a new set with elements common to the set and all others.
intersection_update()Updates the set by keeping only elements found in it and all others.
isdisjoint()Returns True if two sets have no elements in common.
issubset()Checks if the set is a subset of another set and returns True if so.
issuperset()Checks if the set is a superset of another set and returns True if so.
pop()Removes and returns an arbitrary element from the set; raises KeyError if empty.
remove()Removes a specified element from the set; raises KeyError if not present.
symmetric_difference()Returns a new set with elements in either the set or other but not in both.
symmetric_difference_update()Updates the set with elements in either the set or other but not in both.
union()Returns a new set with all elements from the set and all others.
update()Adds all elements from other sets to the set.

Example 1: Using Basic Set Methods

In this example, we will demonstrate the use of add, remove, and clear methods.

Python3

. . . .

Explanation:

  • sports.add('tennis') adds 'tennis' to the set, demonstrating how to include new elements.
  • sports.remove('soccer') removes 'soccer' from the set, showing element removal by value.
  • sports.clear() removes all elements, emptying the set entirely.

Example 2: Using Advanced Set Methods

In this example, we explore union, intersection, and difference methods.

Python3

. . . .

Explanation:

  • union_result = set_a.union(set_b) calculates the union, combining elements from both sets without duplication.
  • intersection_result = set_a.intersection(set_b) finds the intersection, identifying common elements between the two sets.
  • difference_result = set_a.difference(set_b) computes the difference, showing elements unique to set_a.

These examples illustrate how to utilize some of the fundamental set methods in Python to manipulate and compare sets effectively. These operations are crucial for data analysis and mathematical computations involving sets.

Python - set Operations
Quiz
Mark as Completed

On this page

Set Methods

Example 1: Using Basic Set Methods